﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sasa.QualityTools.CodeAnalysis.Common;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.ViewModels;
using Sasa.QualityTools.CodeAnalysis.Core.Models;
using System.ComponentModel;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Views
{
    public partial class CodeMetricsCalculationResultContent : UserControl
    {
        public CodeMetricsCalculationResultContent()
        {
            InitializeComponent();
        }


        public void SaveSetting()
        {
            var model = DataContext as CodeMetricsCalculationResultViewModel;
            model.Grid.Kind.DisplayIndex = kindColumn.DisplayIndex;
            model.Grid.Name.DisplayIndex = nameColumn.DisplayIndex;
            model.Grid.MaintainabilityIndex.DisplayIndex = maintainabilityIndexColumn.DisplayIndex;
            model.Grid.CyclomaticComplexity.DisplayIndex = cyclomaticComplexityColumn.DisplayIndex;
            model.Grid.ClassCoupling.DisplayIndex = classCouplingColumn.DisplayIndex;
            model.Grid.LinesOfCode.DisplayIndex = linesOfCodeColumn.DisplayIndex;
            model.Grid.DepthOfInheritance.DisplayIndex = depthOfInheritanceColumn.DisplayIndex;
            model.Grid.Save();
        }

        public void LoadSetting()
        {
            var model = DataContext as CodeMetricsCalculationResultViewModel;
            model.Grid.Load();
            kindColumn.DisplayIndex = model.Grid.Kind.DisplayIndex;
            nameColumn.DisplayIndex = model.Grid.Name.DisplayIndex;
            maintainabilityIndexColumn.DisplayIndex = model.Grid.MaintainabilityIndex.DisplayIndex;
            cyclomaticComplexityColumn.DisplayIndex = model.Grid.CyclomaticComplexity.DisplayIndex;
            classCouplingColumn.DisplayIndex = model.Grid.ClassCoupling.DisplayIndex;
            linesOfCodeColumn.DisplayIndex = model.Grid.LinesOfCode.DisplayIndex;
            depthOfInheritanceColumn.DisplayIndex = model.Grid.DepthOfInheritance.DisplayIndex;
        }

        private void OnRowDoubleClicked(object sender, MouseButtonEventArgs e)
        {
            var metricValue = ((DataGridRow)sender).DataContext as MetricValueViewModel;
            if (metricValue == null)
            {
                Logger.Warn("Failed to focus because the double-clicked content is not bound to any violation information.");
                return;
            }

            var model = (CodeMetricsCalculationResultViewModel)DataContext;
            var kind = (CodeKind)Enum.Parse(typeof(CodeKind), metricValue.Kind);
            switch (kind)
            {
                case CodeKind.Module:
                    {
                        var query = "p='" + metricValue.ProjectName + "' AND (k='Namespace' OR k='Type' OR k='Member')";
                        model.Search(query);
                        Logger.Info("Search: {0}", query);
                        break;
                    }
                case CodeKind.Namespace:
                    {
                        var query = "p='" + metricValue.ProjectName + "' AND ns='" + metricValue.Namespace + "' AND (k='Type' OR k='Member')";
                        model.Search(query);
                        Logger.Info("Search: {0}", query);
                        break;
                    }
                case CodeKind.Type:
                    {
                        var query = "p='" + metricValue.ProjectName + "' AND ns='" + metricValue.Namespace + "' AND t='" + metricValue.TypeName + "' AND k='Member'";
                        model.Search(query);
                        Logger.Info("Search: {0}", query);
                        break;
                    }
                case CodeKind.Member:
                    {
                        Logger.Info("Began to focus source code.");
                        model.FocusRelatedCode(metricValue);
                    }
                    break;
            }

            e.Handled = true;
        }

        private void OnResetColumns(object sender, RoutedEventArgs e)
        {
            var model = DataContext as CodeMetricsCalculationResultViewModel;
            model.Grid.Reset();
            kindColumn.DisplayIndex = model.Grid.Kind.DisplayIndex;
            nameColumn.DisplayIndex = model.Grid.Name.DisplayIndex;
            maintainabilityIndexColumn.DisplayIndex = model.Grid.MaintainabilityIndex.DisplayIndex;
            cyclomaticComplexityColumn.DisplayIndex = model.Grid.CyclomaticComplexity.DisplayIndex;
            classCouplingColumn.DisplayIndex = model.Grid.ClassCoupling.DisplayIndex;
            linesOfCodeColumn.DisplayIndex = model.Grid.LinesOfCode.DisplayIndex;
            depthOfInheritanceColumn.DisplayIndex = model.Grid.DepthOfInheritance.DisplayIndex;
        }

        private void OnSortRows(object sender, DataGridSortingEventArgs e)
        {
            var model = DataContext as CodeMetricsCalculationResultViewModel;

            if (e.Column.SortDirection == null || e.Column.SortDirection.Value == ListSortDirection.Descending)
            {
                e.Column.SortDirection = ListSortDirection.Ascending;
                model.Sort(e.Column.SortMemberPath);
            }
            else
            {
                e.Column.SortDirection = ListSortDirection.Descending;
                model.Reverse(e.Column.SortMemberPath);
            }

            e.Handled = true;
        }
    }
}
